package com.gimi.cloud.bbp.manage.service.mall.impl;

import com.gimi.cloud.bbp.common.enums.ResponseCodeEnum;
import com.gimi.cloud.bbp.common.exception.MsgException;
import com.gimi.cloud.bbp.common.utils.ReflectUtils;
import com.gimi.cloud.bbp.dao.dao.mall.MallAppSpecialDao;
import com.gimi.cloud.bbp.dao.myMapper.core.query.QueryParam;
import com.gimi.cloud.bbp.dto.mall.MallAppSpecialDTO;
import com.gimi.cloud.bbp.entity.mall.MallAppSpecialEntity;
import com.gimi.cloud.bbp.manage.service.mall.MallAppSpecialService;
import com.github.pagehelper.PageHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 专场管理相关接口实现类
 * @author chendi
 * @date 2018年1月31日
 */
@Service
public class MallAppSpecialServiceImpl implements MallAppSpecialService {
    private static final Logger logger = LoggerFactory.getLogger(MallAppSpecialServiceImpl.class);
    
    private MallAppSpecialDao mallAppSpecialDao;

    @Autowired
    public MallAppSpecialServiceImpl(MallAppSpecialDao mallAppSpecialDao) {
        this.mallAppSpecialDao = mallAppSpecialDao;
    }

    @Override
    public List<MallAppSpecialDTO> queryList(MallAppSpecialDTO dto, boolean isPaging) throws Exception {
        if (isPaging){
            PageHelper.startPage(dto.getPageNums(),dto.getPageSizes());
        }
        String sql = "SELECT asl.* FROM mall_app_special asl WHERE asl.is_delete = 0 AND asl.asg_id = :asgId  ORDER BY asl.asl_sort";
        QueryParam param = new QueryParam();
        param.setSql(sql);
        param.setParamObject(dto);
        return mallAppSpecialDao.selectListDTOBySql(param);
    }

    @Override
    public MallAppSpecialDTO load(MallAppSpecialDTO dto, Object id) throws Exception {
        MallAppSpecialEntity specialEntity = mallAppSpecialDao.queryBy("asl_id",id);
        if(null == specialEntity){
            return null;
        }
        MallAppSpecialDTO specialDTO = new MallAppSpecialDTO();
        ReflectUtils.copyPropertiesIgnoreNull(specialEntity,specialDTO);
        return specialDTO;
    }

    @Override
    public MallAppSpecialDTO saveOrUpdate(MallAppSpecialDTO dto) throws Exception {
        MallAppSpecialEntity specialEntiry = null;
        if (null != dto.getAslId()) {
            specialEntiry = mallAppSpecialDao.queryBy("asl_id", dto.getAslId());
            if (null == specialEntiry) {
            	logger.info("未获取SpecialId为" + dto.getAslId() + "的special信息,更新失败");
                throw new MsgException(ResponseCodeEnum.NO_DATA,"未获取SpecialId为" + dto.getAslId() + "的special信息,更新失败");
            }
        } else {
            specialEntiry = new MallAppSpecialEntity();
        }
        ReflectUtils.copyPropertiesIgnoreNull(dto, specialEntiry);
        if (null != dto.getAslId()) {
            mallAppSpecialDao.baseUpdateByPrimaryKey(specialEntiry);
        } else {
            mallAppSpecialDao.baseInsert(specialEntiry);
        }
        ReflectUtils.copyPropertiesIgnoreNull(specialEntiry, dto);
        return dto;
    }

    @Override
    public int delete(MallAppSpecialDTO dto, Object id, boolean realDel) throws Exception {
        MallAppSpecialEntity specialEntity = mallAppSpecialDao.queryBy("asl_id", id);
        if (null == specialEntity) {
            throw new MsgException(ResponseCodeEnum.NO_DATA,"未获取SpecialId为" + id + "的special信息，删除失败");
        }
        //非物理删除
        if(!realDel){
            ReflectUtils.copyPropertiesIgnoreNull(dto,specialEntity);
            specialEntity.setIsDelete(1);
            return mallAppSpecialDao.baseUpdateByPrimaryKey(specialEntity);
        }
        return mallAppSpecialDao.deleteByPrimaryKey(id);
    }

	@Override
	public int deleteBannerByIds(MallAppSpecialDTO specialDTO, List<Integer> specialItem, Integer asgId) {
		String specialId = "";
        for (Integer str : specialItem) {
            specialId += str + ",";
        }
        String sql = "UPDATE mall_app_special SET is_delete = 1,update_uid = '" + specialDTO.getUid()
                + "',update_uname='" + specialDTO.getUname() + "',update_time =NOW() " +
                " WHERE asg_id =" + specialDTO.getAsgId();
        if(specialId.length() > 0){
            specialId = specialId.substring(0, specialId.length() - 1);
            sql = sql + " AND asl_id not in(" + specialId + ")";
        }
        QueryParam queryParam = new QueryParam();
        queryParam.setSql(sql);
        return mallAppSpecialDao.updateSql(queryParam);
	}
}
